package com.bawei.module_home.ui.sign

import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.lib_storage.AppMMKV
import com.bawei.module_home.R
import com.bawei.module_home.adapter.SignAdapter
import com.bawei.module_home.databinding.ActivitySignsBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlin.math.sign
/**
 *author：yjk
 *description:
 *date：2025/6/16
*/
@AndroidEntryPoint
@Route(path = Router.SIGNS_PAGE)
class SignsActivity : BaseActivity<BaseViewModel,ActivitySignsBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_signs
    private val mViewModel: HomeServiceViewModel by viewModels()
    private val mAdapter = SignAdapter()
    override fun initView() {
        super.initView()
        initButtons()
        initAdapter()
        mBinding.back.setOnClickListener{
            finish()
        }

    }

    override fun initData() {
        super.initData()
        mViewModel.getSignList(AppMMKV.userId)
    }

    override fun initListener() {
        super.initListener()
        mViewModel.signsState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {
                    ToastUtils.showLong(this, "失败"+it.message)
                }
                Loading -> {}
                Success -> {
                    mAdapter.setData(mViewModel.signList)
                }
            }
        }
    }
    fun initButtons(){
        val buttons = arrayOf(R.id.btn_all, R.id.btn_normal, R.id.btn_late, R.id.btn_early, R.id.btn_absent)

        buttons.forEach { id ->
            findViewById<Button>(id).setOnClickListener {
                buttons.forEach { buttonId ->
                    val btn = findViewById<Button>(buttonId)
                    val isSelected = buttonId == id
                    btn.backgroundTintList = ColorStateList.valueOf(
                        if (isSelected){
                            Color.parseColor("#2196F3")
                        } else {
                            Color.WHITE
                        }
                    )
                    btn.setTextColor(if (isSelected) Color.WHITE else Color.BLACK)
                    if (isSelected){
                        when(id){
                            R.id.btn_all -> {
                                mViewModel.getSignList(AppMMKV.userId)
                            }
                            R.id.btn_normal -> {
                                mAdapter.setData(
                                    mViewModel.signList.filter {
                                        it.status == "0"
                                    }
                                )
                            }
                            R.id.btn_late -> {
                                mAdapter.setData(
                                    mViewModel.signList.filter {
                                        it.status == "1"
                                    }
                                )
                            }
                            R.id.btn_early -> {
                                mAdapter.setData(
                                    mViewModel.signList.filter {
                                        it.status == "2"
                                    }
                                )
                            }
                            R.id.btn_absent -> {
                                mAdapter.setData(
                                    mViewModel.signList.filter {
                                        it.status == "3"
                                    }
                                )
                            }
                        }
                    }
                }
            }
        }
    }
    fun initAdapter(){
        mBinding.rvSigns.layoutManager = LinearLayoutManager(this)
        mBinding.rvSigns.adapter = mAdapter
    }


}